package net.controllers;

import java.net.BindException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.model.GridAndBackground;
import net.service.GridAndBackgroundManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/gridReference")
public class GridReferenceController {
	@Autowired
	private GridAndBackgroundManager gridAndBackgrounManagerImpl;

	@RequestMapping(method = RequestMethod.GET)
	public String getUploadForm(Model model) {
		model.addAttribute(gridAndBackgrounManagerImpl.getGridAndBackground());
		return "/gridreference";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String create(GridAndBackground gridAndBackground,
			HttpServletRequest request, HttpServletResponse response,
			Object command, BindException errors, HttpSession session) {
		Double longitudGridReference = null;
		try {
			longitudGridReference = gridAndBackground.getGrid();
		} catch (Exception e) {
			System.out.println("Error: en el parse de la longitud.");
			return "redirect:/forms/gridReference";
		}
		if (longitudGridReference != null) {
			gridAndBackgrounManagerImpl
					.setSizeGridReference(longitudGridReference);
		}
		return "redirect:/forms/home";
	}

}
